package icepuppet.test;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class MyJFrame2 extends JFrame implements MouseListener {

    public MyJFrame2(){
        //设置长宽
        this.setSize(400, 400);
        //设置标题
        this.setTitle("事件练习");
        //设置界面始终位于最上方
        this.setAlwaysOnTop(true);
        //设置界面居中
        this.setLocationRelativeTo(null);
        //设置关闭
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        //取消默认居中
        this.setLayout(null);

        //创建一个按钮
        JButton jButton1 = new JButton("点我呀");
        //设置位置和宽高
        jButton1.setBounds(0,0,100,50);

        //设置鼠标监听
        jButton1.addMouseListener(this);

        //将按钮添加到界面里
        this.getContentPane().add(jButton1);

        //显示
        this.setVisible(true);
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        System.out.println("点击");
    }

    @Override
    public void mousePressed(MouseEvent e) {
        System.out.println("按下不松");
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        System.out.println("松开");
    }

    @Override
    public void mouseEntered(MouseEvent e) {
        System.out.println("划入");
    }

    @Override
    public void mouseExited(MouseEvent e) {
        System.out.println("划出");
    }
}
